#include "display.h"
#include "lcd.h"
#include <stdio.h>
#include <string.h>

float ADCaa[255];
float ADCbb[255];
float ADCgroupAA[255];
float ADCgroupBB[255];

ei CheckCommonError(char * str, uint8_t len)
{
	ei ret = {5, 0, 0};
	if(len > 10)
	{
		return ret;
	}
	
	switch(str[0])
	{
		case 'C': ret.ele = 0;	break;
		case 'L': ret.ele = 1;	break;
		case 'D': ret.ele = 2;	break;
		case 'S': ret.ele = 3;	break;
	}
	if(ret.ele == 3)
	{
		ret.ele = -1;
	} else {
		switch(str[1])
		{
			case '1': ret.no = 1;	break;
			case '2': ret.no = 2;	break;
			case '3': ret.no = 3;	break;
			case '4': ret.no = 4; break;
		}
	}
	if(strstr(str, "Short") == NULL)
	{
		ret.fault = 0;
	} else if(strstr(str, "Break") == 0) {
		ret.fault = 1;
	}
	return ret;
}

void DrawErrorSymbol(ei in)
{
	if(in.ele == 0)
	{
		if(in.no == 1)
		{
			if(in.fault == 1)
			{
				lcd_draw_line(83,285,69,285,RED);//C1,short
				lcd_draw_line(69,285,69,320,RED);
				lcd_draw_line(69,320,83,320,RED);
			}	else {
				lcd_draw_line(72,288,94,317,RED);//C1,break
				lcd_draw_line(72,317,94,288,RED);
			}
		} else if(in.no == 2) {
			if(in.fault == 1)
			{
				lcd_draw_line(145,285,131,285,RED);//C2,short
				lcd_draw_line(131,285,131,320,RED);
				lcd_draw_line(131,320,145,320,RED);
			}	else {
				lcd_draw_line(135,288,156,317,RED);//C2,break
				lcd_draw_line(135,317,156,288,RED);
			}
		} else if(in.no == 3) {
			if(in.fault == 1)
			{
				lcd_draw_line(215,285,201,285,RED);//C3,short
				lcd_draw_line(201,285,201,320,RED);
				lcd_draw_line(201,320,215,320,RED);
			} else {
				lcd_draw_line(205,288,226,317,RED);//C3,break
				lcd_draw_line(205,317,226,288,RED);
			}
		} else if(in.no == 4) {
			if(in.fault == 1)
			{
				lcd_draw_line(278,285,264,285,RED);//C4,short
				lcd_draw_line(264,285,264,320,RED);
				lcd_draw_line(264,320,278,320,RED);
			} else {
				lcd_draw_line(268,288,289,317,RED);//C4,break
				lcd_draw_line(268,317,289,288,RED);
			}
		}
	}	else if(in.ele == 1) {
		if(in.no == 1)
		{
			if(in.fault == 1)
			{
				lcd_draw_line(97,271,97,250,RED);//L1,short
				lcd_draw_line(97,250,132,250,RED);
				lcd_draw_line(132,271,132,250,RED);
			} else {
				lcd_draw_line(104,256,126,285,RED);//L1,break
				lcd_draw_line(104,285,126,256,RED);
			}
		} else if(in.no == 2) {
			if(in.fault == 1)
			{
				lcd_draw_line(167,271,167,250,RED);//L2,short
				lcd_draw_line(167,250,202,250,RED);
				lcd_draw_line(202,271,202,250,RED);
			} else {
				lcd_draw_line(174,256,196,285,RED);//L2,break
				lcd_draw_line(174,285,196,256,RED);
			}
		} else if(in.no == 3) {
			if(in.fault == 1)
			{
				lcd_draw_line(230,271,230,250,RED);//L3,short
				lcd_draw_line(230,250,265,250,RED);
				lcd_draw_line(265,271,265,250,RED);
			} else {
				lcd_draw_line(237,256,259,285,RED);//L3,break
				lcd_draw_line(237,285,259,256,RED);
			}
		}
	} else if(in.ele == 2) {
		lcd_draw_line(49,396,67,396,WHITE);//S
		lcd_draw_line(49,396,67,378,BLUE);
		if(in.no == 1)
		{
			if (in.fault == 0) // short
			{
				lcd_draw_line(41,326,65,355,RED);//D1,break
				lcd_draw_line(41,355,65,326,RED);
			}
		}	else if(in.no == 2) {
			if (in.fault == 0)	// short
			{
				lcd_draw_line(41,354,65,383,RED);//D2,break
				lcd_draw_line(41,383,65,354,RED);
			}
		}
	}	else if(in.ele == -1) {
		if(in.fault == 0)
		{
			lcd_draw_line(49,396,67,396,WHITE);//S
			lcd_draw_line(49,396,67,378,BLUE);
		}
	}
}

void Displaytopic(void)
{
	Chinese_Show_one(10,100,0,32,0);
	Chinese_Show_one(40,100,1,32,0);
	Chinese_Show_one(70,100,2,32,0);
	Chinese_Show_one(100,100,3,32,0);
	Chinese_Show_one(130,100,4,32,0);
	Chinese_Show_one(160,100,5,32,0);
	Chinese_Show_one(190,100,6,32,0);
	Chinese_Show_one(220,100,7,32,0);
	Chinese_Show_one(250,100,8,32,0);
	Chinese_Show_one(280,100,9,32,0);
}

void DisplayButtons(void)
{
	LCD_DrawRectangle(5, 390, 315, 475);
	lcd_show_string(10, 416, 300, 32, 32, "Start the self-test", BLACK);
}

void FlashDisplay(void)
{
	lcd_show_string(5, 55, 200, 32, 32, "Front637:", BLACK);
	lcd_show_string(5, 87, 200, 32, 32, "End637  :", BLACK);
}

void FlashADCDisplay(float ADCgroupA, float ADCgroupB, uint8_t cnt)
{
	if(cnt % 2 == 0)
	{
		sprintf((char *)ADCgroupAA, "%.3f", ADCgroupA);
		lcd_show_string(10,140 + ( (cnt * 40) / 2 ),200,24,24,(char *)ADCgroupAA, BLUE);
		sprintf((char *)ADCgroupBB, "%.3f", ADCgroupB);
		lcd_show_string(75,140 + (	(cnt * 40) / 2 ),200,24,24,(char *)ADCgroupBB, BLUE);
	} else if(cnt % 2 == 1) {
		sprintf((char*)ADCgroupAA,"%.3f ",(float)ADCgroupA);
		lcd_show_string(160,119 + ( (cnt * 40) / 2 ),200,24,24,(char *)ADCgroupAA, BLUE);
		sprintf((char*)ADCgroupBB,"%.3f ",(float)ADCgroupB);
		lcd_show_string(230,119 + ( (cnt * 40) / 2 ),200,24,24,(char *)ADCgroupBB, BLUE);
	}
}

void ADCDisplay(void)
{
	sprintf((char *)ADCaa, "%.3f", (float)ADCfront);
	lcd_show_string(150, 55, 200, 32, 32, (char *)ADCaa, BLUE);
	sprintf((char *)ADCbb, "%.3f", (float)ADCback);
	lcd_show_string(150, 87, 200, 32, 32, (char *)ADCbb, BLUE);
}

void DisplayDetectionMsg(void)
{
	if(strlen(DetectionMsg) == 0 && strlen(DiodeDetectionMsg) == 0)
	{
		Chinese_Show_one(10,150,31,32,0);
		Chinese_Show_one(40,150,32,32,0);
		Chinese_Show_one(70,150,33,32,0);
	} else {
		LCD_Clear(WHITE);
		lcd_show_string(10, 50, 300, 32, 32, DiodeDetectionMsg, BLACK);
		lcd_show_string(10, 100, 300, 32, 32, DetectionMsg, BLACK);
		LCD_Showpicture(0, 250, 320, 154, gImage_TI_s_down);
		ei err_info = CheckCommonError(DetectionMsg, strlen(DetectionMsg));
		if(err_info.ele <= 3)
		{
			DrawErrorSymbol(err_info);
		}
		err_info = CheckCommonError(DiodeDetectionMsg, strlen(DiodeDetectionMsg));
		if(err_info.ele <= 3)
		{
			DrawErrorSymbol(err_info);
		}
	}
}
void countdown(int time)
{
	for(int i = time; i > 0; i--)
	{
		lcd_show_num(270, 440, i, 2, 32, RED);
		HAL_Delay(1000);
	}
}